package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.WorkerDao;
import com.our.sys.project1.entity.Worker;

@Service
@Transactional
public class WorkerService {

    @Autowired
    private WorkerDao workerDao;

    @Autowired
    private HzService hzService;

    private static Logger logger = LoggerFactory.getLogger(WorkerService.class);

    @Transactional(readOnly = true)
    public Page<Worker> query(Page<Worker> page, List<PropertyFilter> filters) {
        Page<Worker> curPage = workerDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Worker queryById(Long id) {
        return workerDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = workerDao.findUnique("select count(a) from Worker a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    @Transactional(readOnly = true)
    private Long queryCountByCode(String code) {
        Long result = workerDao.findUnique("select count(a) from Worker a where a.code = ?", code);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    public void save(Worker worker) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(worker.getName());
            worker.setPy(map.get("py"));
            worker.setWb(map.get("wb"));
            if (null != worker.getId()) {
                Worker getWorker = this.queryById(worker.getId());

                if (queryCountByName(getWorker.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个工作人员名称！");
                }

                if (queryCountByCode(getWorker.getCode()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个工作人员工号！");
                }
                getWorker.setCode(worker.getCode());
                getWorker.setName(worker.getName());
                getWorker.setPy(worker.getPy());
                getWorker.setWb(worker.getWb());
                workerDao.save(getWorker);
            } else {

                if (queryCountByName(worker.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个工作人员名称！");
                }

                if (queryCountByCode(worker.getCode()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个工作人员工号！");
                }
                workerDao.save(worker);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            workerDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
